﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.ServiceModel;

namespace EtlComm
{
    [Serializable]
    public class LogData
    {
        public int Magic = 1978;
        public string StationID;
        public string TaskID;
        public string ErrorMessage;
        public DateTime EventTime;
        public DispatchProcess ProcessState;
        public int ExtractCount;
        public int LoadCount;
    }

    [Serializable]
    public struct DigitalSignature
    {
        public byte[] data;
        public byte[] hashValue;
        public byte[] signature;
    }

    public class BaseLog : ILog
    {
        public string ConnectString { get; set; }

        #region ILog 成员

        public virtual bool WriteLog(string loginfo)
        {
            Debug.WriteLine(loginfo);
            return true;
        }

        public virtual bool WriteLog(string StationID, string TaskID, string loginfo)
        {
            return WriteLog(string.Format("{0}:{1}:{2}",StationID,TaskID,loginfo));
        }

        public virtual bool WriteLog(string StationID, string TaskID, string loginfo, LogType logtype)
        {
            bool bRet = false;
            switch (logtype)
            {
                case LogType.ERROR:
                    {
                        bRet = WriteErrorLog(string.Format("{0}:{1}:{2}", StationID, TaskID, loginfo));
                    }
                    break;
                case LogType.INFO:
                    {
                        bRet = WriteInfoLog(string.Format("{0}:{1}:{2}", StationID, TaskID, loginfo));
                    }
                    break;
                case LogType.WARING:
                    {
                        bRet = WriteInfoLog(string.Format("{0}:{1}:{2}", StationID, TaskID, loginfo));
                    }
                    break;
                case LogType.DEBUG:
                    {
                        bRet = WriteDebugLog(string.Format("{0}:{1}:{2}", StationID, TaskID, loginfo));
                    }
                    break;
                case LogType.FATAL:
                    {
                        bRet = WriteFatalLog(string.Format("{0}:{1}:{2}", StationID, TaskID, loginfo));
                    }
                    break;
                default:
                    break;
            }
            return bRet;
        }

        public virtual bool WriteErrorLog(string szFormat, params object[] Params)
        {
            return WriteLog(string.Format(szFormat, Params));
        }

        public virtual bool WriteInfoLog(string szFormat, params object[] Params)
        {
            return WriteLog(string.Format(szFormat, Params));
        }

        public virtual bool WriteDebugLog(string szFormat, params object[] Params)
        {
            return WriteLog(string.Format(szFormat, Params));
        }

        public virtual bool WriteWarnLog(string szFormat, params object[] Params)
        {
            return WriteLog(string.Format(szFormat, Params));
        }

        public virtual bool WriteFatalLog(string szFormat, params object[] Params)
        {
            return WriteLog(string.Format(szFormat, Params));
        }

        public event EtlProcess OnTaskProcess;

        #endregion

        //public virtual void FireTaskProcess(string StationID, string TaskID, DispatchProcess dp, string Messages)
        //{
        //    EtlProcess tempEP = OnTaskProcess;
        //    if (tempEP != null)
        //    {
        //        OnTaskProcess(this, new EtlEventArgs
        //        {
        //            StationID = StationID,
        //            TaskID = TaskID,
        //            Message = Messages,
        //            EventTime = DateTime.Now,
        //            DProcess = dp
        //        });
        //    }
        //    if (!WriteLog(StationID, TaskID, Messages))
        //    {
        //        throw new ApplicationException("写日志异常...");
        //    }
        //}

        public virtual void FireTaskProcess(string StationID, string TaskID, DispatchProcess dp, string Messages, LogType logtype)
        {
            EtlProcess tempEP = OnTaskProcess;
            if (tempEP != null)
            {
                OnTaskProcess(this, new EtlEventArgs
                {
                    StationID = StationID,
                    TaskID = TaskID,
                    Message = Messages,
                    EventTime = DateTime.Now,
                    DProcess = dp
                });
            }
            if (!WriteLog(StationID, TaskID, Messages,logtype))
            {
                throw new ApplicationException("写日志异常...");
            }
        }

        //public virtual void FireTaskProcess(string StationID, string TaskID, DispatchProcess dp, string Messages, int ExtrCount, int LoadCount)
        //{
        //    EtlProcess tempEP = OnTaskProcess;
        //    if (tempEP != null)
        //    {
        //        OnTaskProcess(this, new EtlEventArgs
        //        {
        //            StationID = StationID,
        //            TaskID = TaskID,
        //            Message = Messages,
        //            EventTime = DateTime.Now,
        //            DProcess = dp,
        //            ExtractCount = ExtrCount,
        //            LoadCount = LoadCount
        //        });
        //    }
        //    if (!WriteLog(StationID, TaskID, Messages))
        //    {
        //        throw new ApplicationException("写日志异常...");
        //    }
        //}
    }
}
